package hjt.com.module.main.activity;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.tong.lib.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import hjt.com.componentbase.constant.ARouterConstants;
import hjt.com.module.main.R;
import hjt.com.module.main.R2;
import hjt.com.module.main.fragment.FirstFragment;

@Route(path = "/main/main")
public class MainActivity extends BaseActivity {

    @BindView(R2.id.fl)
    FrameLayout fl;
    @BindView(R2.id.ll_bottom)
    LinearLayout llBottom;

    private Fragment mCurrentFragment;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected int getLayoutResID() {
        return R.layout.main_activity_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        for (int i = 0; i < llBottom.getChildCount(); i++) {
            //fragmentList初始化
            fragmentList.add(null);
            final int finalI = i;
            //顺便设置子View的点击事件
            llBottom.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTab(finalI);
                }
            });
        }
        /**
         * 要写在fragmentList初始化之后
         */
        setTab(0);
    }

    @Override
    protected void initEvent() {
    }

    private void setTab(int pos) {
        Fragment fragment = getFragment(pos);
        if (fragment != null && mCurrentFragment != fragment) {
            Fragment oldFragment = mCurrentFragment;
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (oldFragment != null) {
                transaction.hide(oldFragment);
            }
            if (fragment.isAdded()) {
                transaction.show(fragment);
            } else {
                transaction.add(R.id.fl, fragment);
            }
            transaction.commit();
            mCurrentFragment = fragment;
        }
    }

    public Fragment getFragment(int pos) {
        //使用fragmentList存储Fragment，不至于产生多个成员变量的fragment影响观看
        if (fragmentList.get(pos) != null) {
            return fragmentList.get(pos);
        }
        Fragment fragment = null;
        switch (pos) {
            case 0:
                fragment = new FirstFragment();
                break;
            case 1:
                fragment = (Fragment) ARouter.getInstance().build(ARouterConstants.FRAGMENT_USER_USER).navigation();
                break;
            case 2:
                fragment = (Fragment) ARouter.getInstance().build(ARouterConstants.FRAGMENT_DESIGN_DESIGN).navigation();
                break;
        }
        fragmentList.set(pos, fragment);
        return fragment;
    }

}
